﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SynoManager.DownloadStation.Plugins
{
    public class TextFileHandler : IDownloadFileHandlerPlugin
    {
        #region IDownloadFileHandlerPlugin Members

        public string Filter
        {
            get { return "*.txt"; }
        }

        public string FileTypeDescription
        {
            get { return "Text files"; }
        }

        public bool IncludeInAllFiles
        {
            get { return true; }
        }

        public void Process(ProcessFileArgs args)
        {
            StreamReader reader = new StreamReader(args.Path);
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                Uri temp;
                if (File.Exists(line))
                    service.AddFile(line);
                else if (Uri.TryCreate(line, UriKind.Absolute, out temp))
                    service.AddUrl(line);
            }
            reader.Close();
        }

        public bool CanProcess(string path)
        {
            // we're able to process all files, since we add only the lines that contain a file path or an url
            return true;
        }
        #endregion

        public void CreateTask(string path)
        {
            throw new NotImplementedException();
        }

        #region IDownloadStationPlugin Members

        private DownloadStationService service;
        public void Initialize(DownloadStationService service)
        {
            this.service = service;
        }

        public void Close()
        {
            
        }

        #endregion
    }
}
